package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.entity.Goods;
import com.school.entity.GoodsCategory;
import com.school.excption.MallException;
import com.school.service.GoodsCategoryService;
import com.school.service.GoodsService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    // 跳转到商品管理页面
    @Resource
    private GoodsCategoryService goodsCategoryService;

    @GetMapping("/goods")
    public String goods(HttpServletRequest request) {
        request.setAttribute("path", "tea_shop_goods");
        return "admin/tea_shop_goods";
    }

    @GetMapping("/goods/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        // 分页操作
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        // 创建业务层对象 根据业务层提供的业务方法做查询操作
        PageResult pageResult = goodsService.getGoodsPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }

    @GetMapping("/goods/edit")
    public String edit(HttpServletRequest request) {
        //查询所有的三级分类的列表
        //查询所有的一级分类
        List<GoodsCategory> firstLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(0L), TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel());
        if (!firstLevelCategories.isEmpty()) {
            //获取一节分类的id
            Long categoryId = firstLevelCategories.get(0).getCategoryId();
            List<GoodsCategory> secondLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId), TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel());
            if (!secondLevelCategories.isEmpty()) {
                Long categoryId1 = secondLevelCategories.get(0).getCategoryId();
                List<GoodsCategory> thirdLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId1), TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
                request.setAttribute("thirdLevelCategories", thirdLevelCategories);
                request.setAttribute("secondLevelCategories", secondLevelCategories);
                request.setAttribute("firstLevelCategories", firstLevelCategories);
                request.setAttribute("path", "tea_shop_goods_edit");
                return "admin/tea_shop_goods_edit";
            }
        }
        MallException.fail("未查询到分类数据");
        return null;
    }

    @PostMapping("/goods/save")
    @ResponseBody
    public Result save(@RequestBody Goods goods) {
        // 判断参数是否为空
        if (StringUtils.isEmpty(goods.getGoodsName()) ||
                StringUtils.isEmpty(goods.getGoodsIntro()) ||
                StringUtils.isEmpty(goods.getGoodsCoverImg())) {
            // 返回参数异常的结果
            return ResultGenerator.genFailResult("参数异常！");
        }
        // 调用goodsService的saveGoods方法保存商品
        String result = goodsService.saveGoods(goods);

        // 判断保存结果是否成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            // 返回成功的结果
            return ResultGenerator.genSuccessResult();
        } else {
            // 返回失败的结果
            return ResultGenerator.genFailResult(result);
        }
    }



}

